/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-08-23 17:55:45
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-07 16:14:50
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { StorePutModelContentDto, StoreFunctionsServiceProxy, StorePutServiceProxy } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';

@Component({
    selector: 'app-manual-add-certificate',
    templateUrl: './manual-add-certificate.component.html',
    styleUrls: ['./manual-add-certificate.component.less']
})
export class ManualAddCertificateComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() warehouseData: any;  //仓库信息
    @Input() title: string;  //标题
    saving = false;
    active = false;
    form = new StorePutModelContentDto();
    model = [];
    sendTime = [];
    modelOptions = [];
    sendTimeOptions = [];
    materialsNodeOptions = [];
    moduleOptions = [];
    unitOptions = [];
    materialCategoryOptions = [];
    binLocationOptions = [];
    materialProperties: Boolean = true;//0=true=正常品  ------------ 2=false=返修品
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _storeFunctionsServiceProxy: StoreFunctionsServiceProxy,
        private _storePutServiceProxy: StorePutServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit() {
    }
    onChange(event) {
        if (this.form.materialsNodeId != undefined) {
            //物料细类
            this._storeFunctionsServiceProxy.getSegmentationMaterialsTypeDropList(
                this.form.materialsNodeId
            ).pipe(finalize(() => { }))
                .subscribe(result => {
                    this.materialCategoryOptions = [];
                    this.materialCategoryOptions = result;
                });
        }
    }
    save() {

        let input = [];
        //如果选择仓库则赋值
        if (this.warehouseData != undefined) {
            this.form.storeId = this.warehouseData.value;
            this.form.stroeName = this.warehouseData.label;
        }
        for (let i = 0; i < this.model.length; i++) {
            let item = this.model[i];
            if (i == 0 && this.model.length - 1 != 0) {
                this.form.productCode = item.value + ',';
            } else if (i == 0 && this.model.length - 1 == 0) {
                this.form.productCode = item.value;
            } else if (i == this.model.length - 1) {
                this.form.productCode = this.form.productCode + item.value;
            } else {
                this.form.productCode = this.form.productCode + item.value + ',';
            }
        }
        for (let i = 0; i < this.sendTime.length; i++) {
            let item = this.sendTime[i];
            if (i == 0 && this.sendTime.length - 1 != 0) {
                this.form.sendTime = item.value + ',';
            } else if (i == 0 && this.sendTime.length - 1 == 0) {
                this.form.sendTime = item.value;
            } else if (i == this.sendTime.length - 1) {
                this.form.sendTime = this.form.sendTime + item.value;
            } else {
                this.form.sendTime = this.form.sendTime + item.value + ',';
            }
        }
        for (const item of this.materialsNodeOptions) {
            if (item.value == this.form.materialsNodeId) {
                this.form.materialsNode = item.label;
            }
        }
        for (const item of this.materialCategoryOptions) {
            if (item.value == this.form.materialCategoryId) {
                this.form.materialCategory = item.label;
            }
        }
        for (const item of this.unitOptions) {
            if (item.value == this.form.unitId) {
                this.form.unit = item.label;
            }
        }
        if (this.materialProperties) {
            this.form.materialsProperty = 0;
        } else {
            this.form.materialsProperty = 1;
        }
        for (const item of this.binLocationOptions) {
            if (item.value == this.form.binLocationId) {
                this.form.binLocation = item.label;
            }
        }
        this.form.warehousMethod = 2;//合格证手工入库
        input.push(this.form)
        if (this.saveFlag) {
            this.saveFlag = false;
            this._storePutServiceProxy.checkYearsNo(
                this.form.productDrawingNo,
                this.form.materialsNodeId
            ).pipe(finalize(() => { }))
                .subscribe(result => {
                    this.saveFlag = true;
                    if (result) {
                        this._storePutServiceProxy.createStorePutContent(input, "B1D77AA5-2F06-4C12-86BE-E60CBB00F991")
                            .pipe(finalize(() => { this.saving = false; }))
                            .subscribe((rs) => {
                                if (rs.code == 0) {
                                    this._messageService.success('保存成功');
                                    this.close();
                                } else {
                                    this._messageService.error(rs.message);
                                }
                            });
                    } else {
                        this.message.confirm(
                            '无年代号，是否继续提交？',
                            (isConfirmed) => {
                                if (isConfirmed) {
                                    this._storePutServiceProxy.createStorePutContent(input, "B1D77AA5-2F06-4C12-86BE-E60CBB00F991")
                                        .pipe(finalize(() => { this.saving = false; }))
                                        .subscribe((rs) => {
                                            if (rs.code == 0) {
                                                this._messageService.success('保存成功');
                                                this.close();
                                            } else {
                                                this._messageService.error(rs.message);
                                            }
                                        });
                                }
                            }
                        );
                    }
                });
            // this._storePutServiceProxy.createStorePutContent(input, "B1D77AA5-2F06-4C12-86BE-E60CBB00F991")
            //     .pipe(finalize(() => { this.saving = false; }))
            //     .subscribe((rs) => {
            //         this.saveFlag = true;
            //         if (rs.code == 0) {
            //             this._messageService.success('保存成功');
            //             this.close()
            //         } else {
            //             this._messageService.error(rs.message);
            //         }
            //         ;
            //     });
        }
    }
    //打开弹窗
    onShown(): void {
        this.active = true;
        this.form.batch = '01';
        //单位
        this._storeFunctionsServiceProxy.getCodeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.unitOptions = [];
                this.unitOptions = result;
            });
        //型号（产品代号）
        this._storeFunctionsServiceProxy.getRocketModeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.modelOptions = [];
                this.modelOptions = result;
            });
        //物料类别
        this._storeFunctionsServiceProxy.getMaterialsTypeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.materialsNodeOptions = [];
                this.materialsNodeOptions = result;
            });
        //储位
        this._storeFunctionsServiceProxy.getBinLocationDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.binLocationOptions = [];
                this.binLocationOptions = result;
            });
    }
    getModuleOptions() {
        //规格
       this._storeFunctionsServiceProxy.getMaterialsContentsModelList(
            this.form.productDrawingNo
           ).pipe(finalize(() => { }))
               .subscribe(result => {
                   this.moduleOptions = [];
                   this.moduleOptions = result;
               });
   }
    getSendTime(event) {
        //发次
        this._storeFunctionsServiceProxy.getRocketLotNoDropList(
            this.model
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.sendTimeOptions = [];
                this.sendTimeOptions = result;
            });
    }
    //查看新增任务
    show(): void {
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.form = new StorePutModelContentDto();
        this.materialCategoryOptions = [];
        this.model = [];
        this.sendTime = [];
        this.active = false;
        this.modalSave.emit();
        this.modal.hide();
    }

}
